<?php
/*
Primary Developer: Alex Arey
Secondary: Derek Shue (primary on oracle conversions)
Updated: 12/11/2011
Module Description: Term provides term-based functionality for searches, queries, and forms.
Serves as both an interface for the terms table and for date-based operations.
*/
class Application_Model_Term{
	public function init(){
	}
	
	/**
	* Returns the current term according to the date, dividing terms as 
	* spring: Jan-May, summer: Jun-Aug, Fall:Sept-Dec
	* 
	* Used to populate current and future terms from the calendar date.
	*/
	public function getCurrentCalendarTerm(){
		$date = getdate();
		$year = $date['year'];
		$month = $date['mon'];
		if($month < 5){
			$curr = array("term"=>'SPRING',"year"=>$year);
		}
		else if($month < 8){
			$curr = array("term"=>'SUMMER',"year"=>$year);
		}
		else{
			$curr = array("term"=>'FALL',"year"=>$year);
		}
		return $curr;
	}
	public function getCurrentCalendarTermStr(){
		$t = $this->getCurrentCalendarTerm();
		return $t['term'].$t['year'];
	}
	
	/**
	* Returns the current open term for submissions accoriding to the terms database table.
	* Valid options for termtype are 'stuapp', 'cpvol','cpintern', and 'facCourse'
	*/
	public function getCurrentOpenTerm($termtype){
		$date = getdate();
		$day = $date['mday'];
		$year = $date['year'];
		$month = $date['mon'];
		$termsTable = new Application_Model_DbTable_Terms();
		$oraDate = $this->parseDateToOracle($month.'/'.$day.'/'.$year);
		if($termtype == 'stuapp'){
			$res = $termsTable->getTermsByDate($oraDate, 'STU');
		}
		else if($termtype == 'cpvol'){
			$res = $termsTable->getTermsByDate($oraDate, 'CPV');
		}
		else if($termtype == 'cpintern'){
			$res = $termsTable->getTermsByDate($oraDate, 'CPI');
		}
		else if($termtype == 'facCourse'){
			$res = $termsTable->getTermsByDate($oraDate, 'FAC');
		}
		else if($termtype == 'admnCourse'){
			$res = $termsTable->getTermsByDate($oraDate, 'ADC');
		}
		else if($termtype == 'admnVol'){
			$res = $termsTable->getTermsByDate($oraDate, 'ADV');
		}
		else if($termtype == 'admnIntern'){
			$res = $termsTable->getTermsByDate($oraDate, 'ADI');
		}
		return $res[0];
	}
	public function getCurrentOpenTermStr($termtype){
		$term = $this->getCurrentOpenTerm($termtype);
		$ret = $term['TERM'].$term['YEAR'];
		return $ret;
	}

	
	/**
	* Returns all terms in the terms table
	*/
	public function getAllTerms(){
		$termsTable = new Application_Model_DbTable_Terms();
		return $termsTable->fetchAll()->toArray();
	}
	/**
	* Returns all terms in the terms table
	*/
	public function getAllTermsStr(){
		$termsTable = new Application_Model_DbTable_Terms();
		$terms =  $termsTable->fetchAll()->toArray();
		$ret = Array();
		foreach($terms as $t){
			$ret[] = $t['TERM'].$t['YEAR'];
		}
		return $ret;
	}

	/**
	* Returns all current and future terms;
	* Current and future terms are determined as terms with dates
	* greater than the current term, with current term and greater than
	* operating as defined in getCurrentTerm() and getTerms().
	*/
	public function getCurrentFutureTermsStr_admin(){
		return $this->getTerms($this->getCurrentCalendarTerm(), "gt");
	}
	
// 	/**
// 	* Returns the current term as a string formatted like 'FALL2011'
// 	*/
// 	public function getCurrentTermStr(){
// 		$curr = $this->getCurrentTerm();
// 		return $curr['term'].$curr['year'];
// 	}
	
	/**
	* Returns all terms that occured before the specified term chronologically.
	* $term should be format array('term'=>'FALL', 'year'=>'2011')
	*/
	public function getTermsBefore($term){
		$termsTable = new Application_Model_DbTable_Terms();
		if($term['term']=='SPRING'){
		    $where = '(YEAR = \''.$term["year"].'\' and TERM in (\'SPRING\')) ';
		}
		else if($term['term']=='SUMMER'){
		    $where = '(YEAR = \''.$term["year"].'\' and TERM in (\'SPRING\', \'SUMMER\')) ';
		}
		else{
		    $where = '(YEAR = \''.$term["year"].'\') ';
		}
		$where = $where . ' or YEAR < \'' . $term['year'].'\'';
		return $termsTable->getTerms($where);
	}	

	/**
	* Returns all terms that occured after the specified term chronologically.
	* $term should be format array('term'=>'FALL', 'year'=>'2011')
	*/
	public function getTermsAfter($term){
		$termsTable = new Application_Model_DbTable_Terms();
		if($term['term']=='FALL'){
		    $where = '(YEAR = \''.$term["year"].'\' and TERM in (\'FALL\')) ';
		}
		else if($term['term']=='SUMMER'){
		    $where = '(YEAR = \''.$term["year"].'\' and TERM in (\'FALL\', \'SUMMER\')) ';
		}
		else{
		    $where = '(YEAR = \''.$term["year"].'\') ';
		}
		$where = $where . ' or YEAR > \'' . $term["year"].'\'';

		return $termsTable->getTerms($where);	
	}
	
    /**
	* Returns all years a given term. e.g. input $term=array('term'=>'FALL') 
	*       returns  FALL2012, FALL2013, FALL2014
	* $term should be format array('term'=>'FALL', 'year'=>'2011')
	* (For anyone maintaining this, yes, we're sorry about the name. It's terrible.)
	*/
	public function getTermsByTerm($term){
		$termsTable = new Application_Model_DbTable_Terms();
		$where = '(TERM = \''.$term['term'].'\') ';
		return $termsTable->getTerms($where);	
	}

	/**
	* Returns true iff the open application dates for the specified term and year 
	* include the current date.
	*/
	public function isAvailableStudentApp($term, $selectedyear){
		$termsTable = new Application_Model_DbTable_Terms();
		$date = getdate();
		$year = $date['year'];
		$month = $date['mon'];
		$day = $date['mday'];
		
		if($month<10)
			$monthstr = "0".$month;
		else
			$monthstr = $month;
			
		if($day<10)
			$daystr = "0".$day;
		else
			$daystr = $day;
		
		$currentdate =  strtotime($monthstr . "/" . $daystr . "/". $year);
		
		$selectedterm = $termsTable->getTerm("TERM = '".$term . "' AND YEAR = ". $selectedyear);
		$begindate = strtotime($selectedterm['STUDENTAPPSTART']);
		$enddate = strtotime($selectedterm['STUDENTAPPEND']);
	
		
		if($currentdate < $begindate|| $currentdate > $enddate){
			return false;
		}
		else{
			return true;
		}
	
	}
	/**
	* Returns true iff the open application dates for the specified term and year 
	* include the current date.
	*/
	public function isAvailableFacultyCourse($term, $selectedyear){
	
		$termsTable = new Application_Model_DbTable_Terms();
		$date = getdate();
		$year = $date['year'];
		$month = $date['mon'];
		$day = $date['mday'];
		
		if($month<10)
			$monthstr = "0".$month;
		else
			$monthstr = $month;
			
		if($day<10)
			$daystr = "0".$day;
		else
			$daystr = $day;
		
		$currentdate =  strtotime($monthstr . "/" . $daystr . "/". $year);
		
		$selectedterm = $termsTable->getTerm("TERM = '".$term . "' AND YEAR = ". $selectedyear);
		$begindate = strtotime($selectedterm['FACULTYCOURSESTART']);
		$enddate = strtotime($selectedterm['FACULTYCOURSEEND']);
	
		
		if($currentdate < $begindate|| $currentdate > $enddate){
			return false;
		}
		else{
			return true;
		}
		
	}
	/**
	* Returns true iff the open application dates for the specified term and year 
	* include the current date.
	* param type: "INTERN" indicates internship period, "VOLUNTEER" indicates volunteer request period. defaults intern
	*/
	public function isAvailablePartnerRequest($term, $selectedyear, $type){
	
		$termsTable = new Application_Model_DbTable_Terms();
		$date = getdate();
		$year = $date['year'];
		$month = $date['mon'];
		$day = $date['mday'];
		
		if($month<10)
			$monthstr = "0".$month;
		else
			$monthstr = $month;
			
		if($day<10)
			$daystr = "0".$day;
		else
			$daystr = $day;
		
		$currentdate =  strtotime($monthstr . "/" . $daystr . "/". $year);
		if(!($iorv == 'INTERN' || $iorv == 'VOLUNTEER')){
			$iorv = 'INTERN';
		}
		$selectedterm = $termsTable->getTerm("TERM = '".$term . "' AND YEAR = ". $selectedyear);
		$begindate = strtotime($selectedterm['PARTNER'.$iorv.'START']);
		$enddate = strtotime($selectedterm['PARTNER'.$iorv.'END']);
	
		
		if($currentdate < $begindate|| $currentdate > $enddate){
			return false;
		}
		else{
			return true;
		}
	
	}
	
	/**
	* Parses dates from MM/DD/YYYY format to the oracle date format DD-Mon-YYYY used by the DB
	*/
	public function parseDateToOracle($date){
	
	$index1 = strpos((string)$date,"/");
	
	if($index1>-1){
	
		$month = substr((string)$date,0,$index1);
		$date2 = substr($date, $index1+1);
		$index2 = strpos((string)$date2,"/");
		if($index2 > -1){
			
			
			$day = substr($date2, 0, $index2);
			$year = substr($date2, $index2 +1); 
				
				
			switch($month){
			
			case "01":
				$monthstr = "JAN";
				break;
			case "02":
				$monthstr = "FEB";
				break;
			case "03":
				$monthstr = "MAR";
				break;
			case "04":
				$monthstr = "APR";
				break;
			case "05":
				$monthstr = "MAY";
				break;
			case "06":
				$monthstr = "JUN";
				break;
			case "07":
				$monthstr = "JUL";
				break;
			case "08":
				$monthstr = "AUG";
				break;
			case "09":
				$monthstr = "SEP";
				break;
			case "10":
				$monthstr = "OCT";
				break;
			case "11":
				$monthstr = "NOV";
				break;
			case "12":
				$monthstr = "DEC";
				break;
			default:
				$monthstr = null;
				break;
			}
				
				if($day&&$monthstr&&$year){
				
				//	echo (string) $day. "-" . $monthstr ."-" . $year;
					return (string) $day. "-" . $monthstr ."-" . $year;
					
					}
				
		}
		
	}
	return null;
}

	/**
	* Parses dates from the oracle date format DD-Mon-YYYY used by the DB to MM/DD/YYYY format
	*/	
public function parseDateFromOracle($date){
	
	$index1 = strpos((string)$date,"-");
	
	if($index1>-1){
	
		$day = substr((string)$date,0,$index1);
		$date2 = substr($date, $index1+1);
		$index2 = strpos((string)$date2,"-");
		
		if($index2 > -1){
			
			
			$month = substr($date2, 0, $index2);
			$year = substr($date2, $index2 + 1); 
				
				
			switch($month){
			
			case "JAN":
				$monthstr = "01";
				break;
			case "FEB":
				$monthstr = "02";
				break;
			case "MAR":
				$monthstr = "03";
				break;
			case "APR":
				$monthstr = "04";
				break;
			case "MAY":
				$monthstr = "05";
				break;
			case "JUN":
				$monthstr = "06";
				break;
			case "JUL":
				$monthstr = "07";
				break;
			case "AUG":
				$monthstr = "08";
				break;
			case "SEP":
				$monthstr = "09";
				break;
			case "OCT":
				$monthstr = "10";
				break;
			case "NOV":
				$monthstr = "11";
				break;
			case "DEC":
				$monthstr = "12";
				break;
			default:
				$monthstr = null;
				break;
			}
				
				if($day&&$monthstr&&$year){
					return (string) $monthstr. "/" . $day ."/" . "20" . $year;
				}
		
		}
		
	}
	return null;
}
	
	/**
	* Returns terms from terms table relative to the current term as string for SQL queries.
	* $term follows same format as other getTerms- methods and
	* $termModifier represents relative positions of terms.
	* $termModifier == 'ns' represents not specified and returns terms before current
	* $termModifier == 'gt' returns getTermsAfter($term)
	* $termModifier == 'lt' returns getTermsBefore($term)
	* $termModifier == 'yo' returns all terms for the same year as $term
	* $termModifier == 'eq' returns a list containing only $term
	* If $term['year']=='*', will return results according to $this->getTermsByTerm($term)
	*/
	public function getTerms($term, $termModifier){
	   if($termModifier == 'ns' || $term['year']==''){
	    //term not specified: get all before current
	        $term = $this->getCurrentCalendarTerm();
	        $termModifier == 'lt';
	    }
		if($term['year']=='*'){
			$terms = $this->getTermsByTerm($term);
	        $term_list = "(";
			foreach($terms as $t){
			    $term_list = $term_list ." '".$t['TERM'] . $t['YEAR']."', ";
			}
			$term_list = trim($term_list,', ') . ")";
		}
	    else if($termModifier == 'lt'){
	        $terms = $this->getTermsBefore($term);
	        $term_list = "(";
	        foreach($terms as $t){
	            $term_list = $term_list ." '".$t['TERM'] . $t['YEAR']."', ";
	        }
	        $term_list = trim($term_list,', ') . ")";
            }
	    else if($termModifier == 'gt'){
	        $terms = $this->getTermsAfter($term);
	        $term_list = "(";
				foreach($terms as $t){
				   $term_list = $term_list ." '".$t['TERM'] . $t['YEAR']."', ";
				}
				$term_list = trim($term_list,', ') . ")";
            }
	    else if($termModifier == 'yo'){
			$term_list = "('SPRING".$term['year']."', 'SUMMER".$term['year']."', 'FALL".$term['year']."')";
	    }
	    else{//($termModifier == 'eq'){
	        $term_list = '(\''.$term['term'].$term['year'].'\')';
	    }
	    return $term_list;
	}

}
?>
